SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[FixedFloating]

@FundID VARCHAR(10)
,@NAVDate DATE

 AS
BEGIN
DECLARE @ExportId INT = (SELECT MAX(e.ExportId)
	            FROM    [FSFundModel].[Reporting].Export e
	            WHERE   e.fundid = @FundID
				AND e.NAVDate = @NAVDate )
DECLARE @MktValSum DECIMAL( 38,10) = (SELECT SUM(a.MktVal) FROM  FSFundModel.Reporting.Asset a WHERE a.ExportId = @ExportId AND a.IsActiveEndOfDay =1) 
DECLARE @tmp TABLE
( RateType VARCHAR(MAX) 
,  OldRateType VARCHAR(MAX) 
)
INSERT INTO @tmp
SELECT 
				CASE WHEN i.RateType LIKE '%Floating%' THEN 'Floating'
				WHEN i.RateType LIKE '%Fixed%' THEN 'Fixed'
				ELSE 'Income Producing/Other' END AS RT
				 , i.RateType
				 FROM [FSFundModel].dbo.Instrument i 
				 GROUP BY i.RateType
				
	/*Dbg
	SET @FundID = 'FSIC'
	*/
	 SELECT    
				tmp.RateType,
				CAST((SUM(a.MktVal)*1000 ) / MAX(@MktValSum) AS DECIMAL (38,10))/1000 AS PctFund

	
	 FROM       [FSFundModel].[Reporting].Asset a
	            INNER JOIN [FSFundModel].[Reporting].Instrument i
	            ON         i.ExportId     = a.ExportId
	            AND        a.InstrumentId = i.InstrumentId
				
				
				
				
					
				--LEFT JOIN  [FSFundModel].[Reporting].FundCreditInstrument fci 
				--ON a.ExportId = fci.ExportId AND i.InstrumentId = fci.InstrumentId
	            
				--INNER JOIN [FSFundModel].[Reporting].Fund f
	   --         ON         a.ExportId = f.ExportId
				INNER JOIN @tmp tmp ON i.RateType = tmp.OldRateType
	 WHERE      a.ExportId                = @ExportId
	            
				AND a.IsActiveEndOfDay =1
	GROUP BY tmp.RateType
END
GO
EXEC sp_addextendedproperty N'VirtualFolder', N'Live/CharlieWaterman', 'SCHEMA', N'dbo', 'PROCEDURE', N'FixedFloating', NULL, NULL
GO
